﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json;
using Terraria.GameContent.UI.Elements;
using Terraria.GameContent.UI.States;
using Terraria.Localization;
using Terraria.UI;
using Terraria.UI.Gamepad;

namespace Terraria.ModLoader.UI
{
	// Token: 0x020002A7 RID: 679
	internal class UIModPacks : UIState
	{
		// Token: 0x06001B11 RID: 6929 RVA: 0x0042E828 File Offset: 0x0042CA28
		public override void OnInitialize()
		{
			UIElement uielement = new UIElement();
			uielement.Width.Set(0f, 0.8f);
			uielement.MaxWidth.Set(600f, 0f);
			uielement.Top.Set(220f, 0f);
			uielement.Height.Set(-220f, 1f);
			uielement.HAlign = 0.5f;
			this.uiLoader = new UILoaderAnimatedImage(0.5f, 0.5f, 1f);
			this.scrollPanel = new UIPanel();
			this.scrollPanel.Width.Set(0f, 1f);
			this.scrollPanel.Height.Set(-65f, 1f);
			this.scrollPanel.BackgroundColor = new Color(33, 43, 79) * 0.8f;
			uielement.Append(this.scrollPanel);
			this.modListList = new UIList();
			this.modListList.Width.Set(-25f, 1f);
			this.modListList.Height.Set(0f, 1f);
			this.modListList.ListPadding = 5f;
			this.scrollPanel.Append(this.modListList);
			UIScrollbar uiscrollbar = new UIScrollbar();
			uiscrollbar.SetView(100f, 1000f);
			uiscrollbar.Height.Set(0f, 1f);
			uiscrollbar.HAlign = 1f;
			this.scrollPanel.Append(uiscrollbar);
			this.modListList.SetScrollbar(uiscrollbar);
			UITextPanel<string> uitextPanel = new UITextPanel<string>(Language.GetTextValue("tModLoader.ModPacksHeader"), 0.8f, true);
			uitextPanel.HAlign = 0.5f;
			uitextPanel.Top.Set(-35f, 0f);
			uitextPanel.SetPadding(15f);
			uitextPanel.BackgroundColor = new Color(73, 94, 171);
			uielement.Append(uitextPanel);
			UITextPanel<string> uitextPanel2 = new UITextPanel<string>(Language.GetTextValue("UI.Back"), 1f, false);
			uitextPanel2.Width.Set(-10f, 0.5f);
			uitextPanel2.Height.Set(25f, 0f);
			uitextPanel2.VAlign = 1f;
			uitextPanel2.Top.Set(-20f, 0f);
			uitextPanel2.OnMouseOver += UICommon.FadedMouseOver;
			uitextPanel2.OnMouseOut += UICommon.FadedMouseOut;
			uitextPanel2.OnClick += UIModPacks.BackClick;
			uielement.Append(uitextPanel2);
			UIColorTextPanel uicolorTextPanel = new UIColorTextPanel(Language.GetTextValue("tModLoader.ModPacksSaveEnabledAsNewPack"), Color.Green, 1f, false);
			uicolorTextPanel.CopyStyle(uitextPanel2);
			uicolorTextPanel.HAlign = 1f;
			uicolorTextPanel.OnMouseOver += UICommon.FadedMouseOver;
			uicolorTextPanel.OnMouseOut += UICommon.FadedMouseOut;
			uicolorTextPanel.OnClick += UIModPacks.SaveNewModList;
			uielement.Append(uicolorTextPanel);
			base.Append(uielement);
		}

		// Token: 0x06001B12 RID: 6930 RVA: 0x0042EB40 File Offset: 0x0042CD40
		private static void SaveNewModList(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.MenuUI.SetState(new UIVirtualKeyboard(Language.GetTextValue("tModLoader.ModPacksEnterModPackName"), "", new UIVirtualKeyboard.KeyboardSubmitEvent(UIModPacks.SaveModList), delegate
			{
				Main.menuMode = 10016;
			}, 0, false));
			Main.menuMode = 888;
		}

		// Token: 0x06001B13 RID: 6931 RVA: 0x0042EBB8 File Offset: 0x0042CDB8
		public static void SaveModList(string filename)
		{
			Directory.CreateDirectory(UIModPacks.ModListSaveDirectory);
			string path = UIModPacks.ModListSaveDirectory + Path.DirectorySeparatorChar.ToString() + filename + ".json";
			List<string> value = (from x in ModLoader.FindMods()
			select x.Name).Intersect(ModLoader.EnabledMods).ToList<string>();
			string contents = JsonConvert.SerializeObject(value, Formatting.Indented);
			File.WriteAllText(path, contents);
			Main.menuMode = 10016;
		}

		// Token: 0x06001B14 RID: 6932 RVA: 0x000135AB File Offset: 0x000117AB
		private static void BackClick(UIMouseEvent evt, UIElement listeningElement)
		{
			Main.PlaySound(11, -1, -1, 1, 1f, 0f);
			Main.menuMode = 10000;
		}

		// Token: 0x06001B15 RID: 6933 RVA: 0x000135CC File Offset: 0x000117CC
		public override void Draw(SpriteBatch spriteBatch)
		{
			base.Draw(spriteBatch);
			UILinkPointNavigator.Shortcuts.BackButtonCommand = 100;
			UILinkPointNavigator.Shortcuts.BackButtonGoto = 10000;
		}

		// Token: 0x06001B16 RID: 6934 RVA: 0x0042EC40 File Offset: 0x0042CE40
		public override void OnActivate()
		{
			this.scrollPanel.Append(this.uiLoader);
			this.modListList.Clear();
			if (SynchronizationContext.Current == null)
			{
				SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
			}
			Task.Factory.StartNew<string[]>(delegate
			{
				UIModPacks.mods = (from m in ModLoader.FindMods()
				select m.Name).ToArray<string>();
				return this.FindModLists();
			}).ContinueWith(delegate(Task<string[]> task)
			{
				string[] result = task.Result;
				foreach (string path in result)
				{
					string[] array2 = new string[0];
					if (File.Exists(path))
					{
						using (StreamReader streamReader = new StreamReader(path))
						{
							string value = streamReader.ReadToEnd();
							array2 = JsonConvert.DeserializeObject<string[]>(value);
						}
					}
					UIModPackItem item = new UIModPackItem(Path.GetFileNameWithoutExtension(path), array2);
					this.modListList.Add(item);
				}
				this.scrollPanel.RemoveChild(this.uiLoader);
			}, TaskScheduler.FromCurrentSynchronizationContext());
		}

		// Token: 0x06001B17 RID: 6935 RVA: 0x0042ECA8 File Offset: 0x0042CEA8
		private string[] FindModLists()
		{
			Directory.CreateDirectory(UIModPacks.ModListSaveDirectory);
			return Directory.GetFiles(UIModPacks.ModListSaveDirectory, "*.json", SearchOption.TopDirectoryOnly);
		}

		// Token: 0x04001842 RID: 6210
		private UIList modListList;

		// Token: 0x04001843 RID: 6211
		private UILoaderAnimatedImage uiLoader;

		// Token: 0x04001844 RID: 6212
		private UIPanel scrollPanel;

		// Token: 0x04001845 RID: 6213
		internal static string ModListSaveDirectory = ModLoader.ModPath + Path.DirectorySeparatorChar.ToString() + "ModPacks";

		// Token: 0x04001846 RID: 6214
		internal static string[] mods;
	}
}
